//
//  LDJobfinderDetailBaseVCInAll_Show.swift
//  DingDongZP
//
//  Created by LeMac on 17/1/11.
//  Copyright © 2017年 51ledian.com. All rights reserved.
//

import UIKit
@objc
class LDJobfinderDetailBaseVCInAll_Show: LDMyResumeViewControllerInOne_Show {

    
    var userID:String?
    //btn
    var btn1:UIButton!
    var btn2:UIButton!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    override func setupView() {
        
        super.setupView()
        setupNav()
        setupBtn()
        tableView?.contentInset = UIEdgeInsetsMake(0, 0, BtnH, 0)
        
    }
    
    //请求微简历
    override func sendRequest(){
        
        sendRequest(userID)
    }
    
    func sendRequest(userid:String?){
        
        if userid == nil { return }
        
        //请求微简历信息
        let intid:Int = Int(userid!) ?? 0
        LDBossInfoNet.sharedInstance.queryJobFinderMyResume(intid, success: {
            [weak self] (dict) in
            
            var newDict = dict
            
            //适配 后台返回值 不一致
            let industryArr = dict["expectjobindustries"] as? NSArray
            var newIndustryArr = [NSDictionary]()
            if industryArr != nil {
                
                newIndustryArr = industryArr!.map({ (obj) -> NSDictionary in
                    
                    return NSDictionary.init(object: obj, forKey: "industry")
                })
                
                newDict["expectjobindustries"] = newIndustryArr
            }
            
            let highlightsArr = dict["highlights"] as? NSArray
            var newHighlightsArr = [NSDictionary]()
            if highlightsArr != nil {
                
                newHighlightsArr = highlightsArr!.map({ (obj) -> NSDictionary in
                    
                    return NSDictionary.init(object: obj, forKey: "tag")
                })
                
                newDict["highlights"] = newHighlightsArr
            }
            
            
            let workExpArr = dict["workexperience"] as? [NSDictionary]
            var newWorkExpArr = [NSDictionary]()
            
            if workExpArr != nil {
                
                
                var newItem = NSMutableDictionary()
                
                for (_,item) in workExpArr!.enumerate() {
                    
                    
                    newItem = NSMutableDictionary.init(dictionary: item)

                    guard let newStrArr = item["jobstrenths"] as? [String] else { return }
                    
                    let newDicArr = newStrArr.map({ (obj) -> NSDictionary in
                        
                        return ["tag":obj]
                    })
                    
                    newItem["jobstrenths"] = newDicArr
                    
                    newWorkExpArr.append(newItem)
                }
                
                newDict["workexperience"] = newWorkExpArr
                
                //遍历 dic 数组
                for (i,item) in workExpArr!.enumerate() {
                    
                    var rowJobDicArr = NSDictionary.init(dictionary: workExpArr![i])
                    
                    //每个dic job 数组
                    let workExpJobStrength = item["jobstrenths"] as? [String]
                    
                    var newWorkExpJobStrength = [NSDictionary]()
                    
                    if workExpJobStrength != nil {
                        
                    
                        //替换job数组
                        for item in workExpJobStrength! {
                            
                            let dict = ["tag":item]
                            newWorkExpJobStrength.append(dict)
                        }
                    }
                    
                    let dictArr = rowJobDicArr["jobstrenths"] as?[NSDictionary]
                }
            }
            
            self?.model = DDMyResumeModel.yy_modelWithDictionary(newDict)
            
        }, failure: nil)
        
        //请求是关注
        
        DDJobfindersService.shareInstance().queryJobfindersInterestingStatusWithJobfindeId(userid, successBlock: {
            [weak self] (data) in
            
            let dict = data as? NSDictionary
            let isInterest = dict!["data"] as? Int
            
            if isInterest == 1 {
                
                self?.btn1.selected = true
            } else {
                
                self?.btn1.selected = false
            }
            
            }, failBlock: nil)
    }
    
    
    func setupNav() {
        
        
        self.tableView?.tableHeaderView = nil
        
        let rightview = UIView.init(frame: CGRectMake(0, 0, 64, 44))
        
        let repbtn = UIButton.setDetailsBtn(nil, picName: "common_nav_report", btnBackColor: nil, titleColor: nil)
        repbtn?.frame = CGRectMake(32, 1, 42, 42)
        //        let shareBtn = UIButton.setDetailsBtn(nil, picName: "common_nav_share", btnBackColor: nil, titleColor: nil)
        
        rightview.addSubview(repbtn!)
        
        let rightbar = UIBarButtonItem(customView: rightview)
        navigationItem.rightBarButtonItem = rightbar
        
        //举报
        repbtn?.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext({ [weak self](obj) in
            
            LDCheckPublicInfo.shareInstance.checkoutReportJobfinderList({ (data) in
                
                var titltArr = [String]()
                var titleArrID = [String]()
                
                for item in data {
                    
                    titltArr.append(item["complaint_text"] as? String ?? LDNull)
                    titleArrID.append(item["complaint_id"] as? String ?? LDNull)
                }
                
                
                //显示action
                self?.showLDActionSheetView("举报牛人", labHeight: nil, labsTitleArr: titltArr, selClosure: { [weak self](index) in
                    
                    let params = ["jobfinder_id":self?.userID ?? "0","complaint_id":titleArrID[index]]
                    
                    //
                    LDBossInfoNet.sharedInstance.reportJobfinder(params, success: {
                        
                        LDShowHUB.shareInstance.showSucc("举报成功")
                        
                        }, failure: { (error) in
                            
                        LDShowHUB.shareInstance.showToastNoti("举报失败")
                    })
                })
            })
            })
    }
    
}

extension LDJobfinderDetailBaseVCInAll_Show {
    
    //底部编辑按钮
    func setupBtn() {
        
        
//        self.rdv_tabBarController.setTabBarHidden(true, animated: false)
        let divideLine1 = UIView()
        divideLine1.backgroundColor = LDIconColor.LightGray
        
        let divideLine2 = UIView()
        divideLine2.backgroundColor = LDIconColor.LightGray
        
        btn1 = UIButton.setBtn("感兴趣", picName: "interest.png", btnBackColor: LDFontColor.White, titleColor: LDFontColor.Orange)!
        btn1.setImage(UIImage(named: "interested.png"), forState: .Selected)
        btn1.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0)
        btn1.titleLabel?.font = LDFont.Font16
        btn1.layer.cornerRadius = 0
        
        btn1.bk_whenTapped { [weak self] in
            
            DDJobfindersService.shareInstance().markJobfindersInterestingStatusWithJobfindeId(self?.userID ?? "", successBlock: { [weak self] (data) in
                
                let dict = data as? NSDictionary
                let isInterest = dict!["data"] as? Int
                
                if isInterest == 1 {
                    LDShowHUB.shareInstance.showToastSucc("关注成功")
                    self?.btn1.selected = true
                } else {
                    LDShowHUB.shareInstance.showToastSucc("取消关注")
                    self?.btn1.selected = false
                }
                
                }, failBlock: nil)
            
        }
        
        btn2 = UIButton.setDetailsBtn("立即沟通", picName: "沟通.png", btnBackColor: LDFontColor.White, titleColor: LDGolbalColor.Green)!
        btn2.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0)
        btn2.titleLabel?.font = LDFont.Font16
        btn2.layer.cornerRadius = 0
        
        
        btn2.bk_whenTapped { [weak self] in
            
            if self?.userID == nil {
                LDShowHUB.shareInstance.showToastNoti("用户信息获取失败")
                return
            }
            
            let vc = DDBossChoosePositionViewController()
            vc.targetID = (self?.userID)!+"_jobfinder"
            vc.titleName = self?.model?.name
            self?.navigationController?.pushViewController(vc, animated: true)
        }
        
        view.addSubview(btn1)
        view.addSubview(btn2)
        view.addSubview(divideLine1)
        view.addSubview(divideLine2)
        
        btn1.snp_makeConstraints { (make) in
            make.left.bottom.equalTo(0)
            make.right.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        
        btn2.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(0)
            make.left.equalTo(self.view.snp_centerX).offset(0)
            make.height.equalTo(BtnH)
        }
        divideLine1.snp_makeConstraints { (make) in
            make.top.equalTo(btn1)
            make.right.left.equalTo(0)
            make.height.equalTo(0.8)
        }
        divideLine2.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.centerY.equalTo(btn1)
            
            make.height.equalTo(BtnH - LDMargin_V*2)
            make.width.equalTo(0.8)
        }
    }
    //感兴趣
    func manageExpextJob() {
        
        let vc = LDManageMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    //对话
    func editMyResume() {
        
        let vc = editMyResumeVC()
        navigationController?.pushViewController(vc, animated: true)
    }
}
